<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <title>菜鸟教程(runoob.com)</title>

    <script type="text/javascript">
        function WebSocketTest()
        {
            // 打开一个 web socket
            var ws = new WebSocket("ws://175.178.132.26:9501");
            window.wsobj = ws;
            ws.onopen = function()
            {
                // Web Socket 已连接上，使用 send() 方法发送数据
            };

            ws.onmessage = function (evt)
            {
                var received_msg = evt.data;
                console.log(received_msg);
            };

            ws.onclose = function()
            {
                // 关闭 websocket
                console.log("连接已关闭...");
            };

        }
        
        function sendMsg() {
            
            if (window.wsobj.readyState==1){
                let msg = "王跃是臭弟弟吗";
                console.log(msg.length);
                window.wsobj.send(msg);
            }
        }

        function close() {
            if (window.wsobj.readyState==1){
                window.wsobj.close();
            }
        }
    </script>

</head>
<body>

<div id="sse">
    <a href="javascript:WebSocketTest()">运行 WebSocket</a>
    <a href="javascript:sendMsg()">sendMsg</a>
    <a href="javascript:close()">close</a>

</div>

</body>
</html>